package org.lai.controller;

import lombok.extern.slf4j.Slf4j;
import org.lai.pojo.PageResult;
import org.lai.pojo.Result;
import org.lai.pojo.Student;
import org.lai.pojo.StudentQueryParm;
import org.lai.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result getList(StudentQueryParm student) {
        log.info("分页查询:" + student);
        PageResult<Student> list = studentService.getList(student);
        return Result.success(list);
    }

    /**
     * 新增学员
     */
    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        log.info("新增学员信息:{}", student);
        studentService.addStudent(student);
        return Result.success();
    }

    /**
     * 根据id查询学员信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学员信息:{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息:{}", student);
        studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * 删除学员信息
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除id为的学员{}:", id);
        studentService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改学员处分
     */
    @PutMapping("/violation/{id}/{score}")
    public Result updateScore(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("修改学员id为{}的分数为{}", id, score);
        studentService.updateStudent(id, score);
        return Result.success();
    }
}
